using B.S.XZYData.Api.Wrter.Application.Command.WorkReport;
using B.S.XZYData.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;

namespace B.S.XZYData.Api.Wrter.Controllers
{
	/// <summary>
	/// 报工信息写入接口
	/// </summary>
	[ApiController]
	[Route("api/[controller]/[action]")]
	public class WorkReportController : ControllerBase
	{
		private readonly IMediator _mediator;

		public WorkReportController(IMediator mediator)
		{
			_mediator = mediator;
		}

		/// <summary>
		/// 批量删除报工（伪删除）
		/// </summary>
		/// <param name="request">包含要删除的报工ID列表</param>
		/// <returns>删除结果</returns>
		[HttpDelete]
		public Task<ApiResult<int>> DeleteBatch([FromBody] DeleteWorkReportBatchCommand request)
		{
			return _mediator.Send(request);
		}

		/// <summary>
		/// 编辑报工（含不良品项）
		/// </summary>
		[HttpPut]
		public Task<ApiResult<int>> Edit([FromBody] EditWorkReportCommand request)
		{
			return _mediator.Send(request);
		}

		/// <summary>
		/// 新增报工（含不良品项）
		/// </summary>
		[HttpPost]
		public Task<ApiResult<int>> Add([FromBody] AddWorkReportCommand request)
		{
			return _mediator.Send(request);
		}

		/// <summary>
		/// 导出报工数据到Excel
		/// </summary>
		/// <param name="processName">工序名称</param>
		/// <param name="productCode">产品编号</param>
		/// <param name="productName">产品名称</param>
		/// <param name="processStatus">工序状态</param>
		/// <param name="producer">生产人员</param>
		/// <param name="workOrderCode">工单编号</param>
		/// <param name="startDate">开始日期</param>
		/// <param name="endDate">结束日期</param>
		/// <param name="exportFormat">导出格式</param>
		/// <returns>Excel文件流</returns>
		[HttpGet]
		public async Task<IActionResult> Export(
			[FromQuery] string? processName = null,
			[FromQuery] string? productCode = null,
			[FromQuery] string? productName = null,
			[FromQuery] int? processStatus = null,
			[FromQuery] string? producer = null,
			[FromQuery] string? workOrderCode = null,
			[FromQuery] string? startDate = null,
			[FromQuery] string? endDate = null,
			[FromQuery] string? exportFormat = "xlsx")
		{
			try
			{
				// 转换时间参数
				DateTime? startDateTime = null;
				DateTime? endDateTime = null;
				
				if (!string.IsNullOrWhiteSpace(startDate) && DateTime.TryParse(startDate, out var start))
					startDateTime = start;
				
				if (!string.IsNullOrWhiteSpace(endDate) && DateTime.TryParse(endDate, out var end))
					endDateTime = end;

				var request = new ExportWorkReportCommand
				{
					ProcessName = processName,
					ProductCode = productCode,
					ProductName = productName,
					ProcessStatus = processStatus,
					Producer = producer,
					WorkOrderCode = workOrderCode,
					StartDate = startDateTime,
					EndDate = endDateTime,
					ExportFormat = exportFormat ?? "xlsx"
				};

				var result = await _mediator.Send(request);
				
				if (result.Code != ApiEnum.Success)
				{
					return BadRequest(new { message = result.Msg });
				}

				var fileName = $"报工信息_{DateTime.Now:yyyyMMddHHmmss}.xlsx";
				
				return File(result.Data, 
					"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", 
					fileName);
			}
			catch (Exception ex)
			{
				return BadRequest(new { message = $"导出失败：{ex.Message}" });
			}
		}
	}
} 